package com.fenghongzhang.day013;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ImageUtil.MyListener {
    private Button btn;
    private ImageView img;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        String arrs ="[{name:\"小明\",age:18,address:\"北京\",sex=\"男\"},{name:\"大明\",age:20,address:\"上海\",sex=\"女\"}]";


        Gson gson = new Gson();
        JsonParser jsonParser = new JsonParser();
        JsonArray asJsonArray = jsonParser.parse(arrs).getAsJsonArray();

        List<Person> list = new ArrayList<>();
        for (int i = 0; i < asJsonArray.size(); i++) {
            JsonElement jsonElement = asJsonArray.get(i);
            Person person = gson.fromJson(jsonElement.toString(), Person.class);
            Log.i(TAG, "onCreate: "+person);
            list.add(person);
        }


        btn = (Button) findViewById(R.id.btn);
        img = (ImageView) findViewById(R.id.img);

        ImageUtil instance = ImageUtil.getInstance();
        instance.setMyListener(this);
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},100);
    }



    public void loading(View view) {
        ImageUtil.getInstance().loading("http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg");
    }

    @Override
    public void click(Bitmap bitmap) {
        img.setImageBitmap(bitmap);
    }
}
